package com.jsmy.marketing.fg;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.CustomerActivity;
import com.jsmy.marketing.adapter.DailyFragmentAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.YWgtjhBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vivian.timelineitemdecoration.itemdecoration.DotItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by PC_WLT on 2018/8/27.
 */

public class CustomerDailyFragment extends BaseFragment {
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    DailyFragmentAdapter adapter;
    List<YWgtjhBean.DataBean.ListBean> list;
    int pageindex = 1;
     DotItemDecoration mItemDecoration;
    @Override
    protected int resLayout() {
        return R.layout.fg_test_layout;
    }

    @Override
    protected void initData() {
        adapter = new DailyFragmentAdapter();
//        myRecycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//        mItemDecoration = new DotItemDecoration
//                .Builder(getActivity())
//                .setOrientation(DotItemDecoration.VERTICAL)//if you want a horizontal item decoration,remember to set horizontal orientation to your LayoutManager
//                .setItemStyle(DotItemDecoration.STYLE_DRAW)
//                .setTopDistance(2)//dp
//                .setItemInterVal(5)//dp
//                .setItemPaddingLeft(2)//default value equals to item interval value
//                .setItemPaddingRight(2)//default value equals to item interval value
//                .setDotColor(Color.parseColor("#009688"))
//                .setDotRadius(2)//dp
//                .setDotPaddingTop(0)
//                .setDotInItemOrientationCenter(false)//set true if you want the dot align center
//                .setLineColor(Color.parseColor("#2ecc71"))
//                .setLineWidth(1)//dp
//                .setEndText("END")
//                .setTextColor(Color.WHITE)
//                .setTextSize(10)//sp
//                .setDotPaddingText(2)//dp.The distance between the last dot and the end text
//                .setBottomDistance(40)//you can add a distance to make bottom line longer
//                .create();
//        mItemDecoration.setSpanIndexListener(new SpanIndexListener() {
//            @Override
//            public void onSpanIndexChange(View view, int spanIndex) {
//                Log.i("Info","view:"+view+"  span:"+spanIndex);
//                view.setBackgroundResource(spanIndex == 0 ? R.drawable.pop_left : R.drawable.pop_right);
//            }
//        });
//        myRecycler.addItemDecoration(mItemDecoration);
        myRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
               getkhzlgtrzlist(((CustomerActivity)getActivity()).kid,pageindex);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
               getkhzlgtrzlist(((CustomerActivity)getActivity()).kid,pageindex);

            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(getActivity());
                normalDialog.setMessage("确定删除该条日志吗？");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                savedelgtrzinfo(list.get(position).getRid());
                            }
                        });
                normalDialog.setNegativeButton("关闭",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //...To-do
                            }
                        });
                // 显示
                normalDialog.show();
            }
        });
        update();
    }

    private void savedelgtrzinfo(String rid) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("rid", rid);
        mPresenter.loadData(RetrofitManager.getInstance().savedelgtrzinfo(map), Link.APP_SAVE_DEL_GTRZ_INFO);

    }

    private void getkhzlgtrzlist(String kid, int pageindex) {

        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("kid", kid);
        map.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().getkhzlgtrzlist(map), Link.APP_KHZL_GTRZ_LIST);
    }

    @Override
    public void refreshData(Object object, String type) {
        switch (type){
            case Link.APP_KHZL_GTRZ_LIST:
                list = ((YWgtjhBean)object).getData().getList();
                if (pageindex == 1){
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                }else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
            case Link.APP_SAVE_DEL_GTRZ_INFO:
                ToastUtil.showShort(getContext(),"删除成功！");
                update();
                break;
        }
    }
    @Override
    public void update(){
        pageindex=1;
        getkhzlgtrzlist(((CustomerActivity)getActivity()).kid,pageindex);
    }
}
